package com.liu.usky.rlzp.uskyrlzp.controller;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.liu.usky.rlzp.uskyrlzp.entity.Equity;
import com.liu.usky.rlzp.uskyrlzp.entity.RecruitmentInfos;
import com.liu.usky.rlzp.uskyrlzp.entity.User;
import com.liu.usky.rlzp.uskyrlzp.service.IRecruitmentInfosService;
import com.liu.usky.rlzp.uskyrlzp.util.DateUtils;
import com.liu.usky.rlzp.uskyrlzp.util.R;

/**
 * <p>
 * 招聘信息 前端控制器
 * </p>
 *
 * @author lcjzl
 * @since 2020-01-10
 */
@RestController
@RequestMapping("/uskyrlzp/recruitment-infos")
public class RecruitmentInfosController {
	@Autowired
	private IRecruitmentInfosService iRecruitmentInfosService; 
	
	@PostMapping(value="addRecruInfos",produces="application/json")
	public String addRecruInfos(@RequestBody RecruitmentInfos reinfos,HttpServletRequest request ) throws JsonProcessingException {
		User user =(User) request.getSession().getAttribute("user");
		if(user.getUserType() != 1) {
			return R.FAILURE("非企业用户，不能发布招聘",null);
		}
		List<Equity> equitys = user.getEquitys();
		Integer limitPersons = 0;
		for (int i = 0; i < equitys.size(); i++) {
			if(equitys.get(i).getType() == 1) {// 招聘人数上限
				limitPersons = equitys.get(i).getValue();
			}
		}
		if(limitPersons == 0) {
			return R.FAILURE("可招聘人数为0，请购买权益提高招聘人数",null);
		}
		if(limitPersons < reinfos.getNeedPersons()) {
			reinfos.setNeedPersons(limitPersons);
		}
		reinfos.setPublishLimitPersons(limitPersons);
		reinfos.setStatus(0);
		if(StringUtils.isEmpty(reinfos.getDeadline())) {
			reinfos.setDeadline(DateUtils.getTomorrowEndOfDayDate());
		}
		reinfos.setCreateTime(new Date());
		reinfos.setUserId(user.getId());
		reinfos.setPublicPerson(user.getCompany());
		reinfos.setRemainder(reinfos.getNeedPersons());
		reinfos.setCode(com.liu.usky.rlzp.uskyrlzp.util.StringUtils.getRecuritInfosCode());
		boolean save = iRecruitmentInfosService.save(reinfos);
		if(save) {
			return R.OK("新增成功");
		}
		return R.FAILURE("新增失败", null);
	}

}
